package com.kele.goods.controller;

import com.kele.api.goods.TypeTemplateControllerApi;
import com.kele.common.model.response.PageResult;
import com.kele.common.model.response.Result;
import com.kele.goods.service.TypeTemplateService;
import com.kele.model.goods.TypeTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/template")
public class TypeTemplateController implements TypeTemplateControllerApi {

    @Autowired
    TypeTemplateService templateService;

    @Override
    @GetMapping("findAll")
    public List<TypeTemplate> findAll(){
        return templateService.findAll();
    }

    @Override
    @PostMapping("findPage/{page}/{size}")
    public PageResult<TypeTemplate> findPage(@PathVariable int page, @PathVariable int size,
                                             @RequestBody Map searchMap) {
        return templateService.findPage(page,size,searchMap);
    }

    @Override
    @GetMapping("{id}")
    public TypeTemplate findById(@PathVariable Long id) {
        return templateService.findById(id);
    }

    /**
     * @param typeTemplate id为null，保存，id！=null 更新
     * @return
     */
    @Override
    @PostMapping("save")
    public Result save(@RequestBody TypeTemplate typeTemplate) {
        if(typeTemplate!=null){
            if(typeTemplate.getId()==null){
                templateService.add(typeTemplate);
                return new Result(true,"1","新增成功");
            }else{
                templateService.update(typeTemplate);
                return new Result(true,"1","修改成功");
            }
        }
        return Result.FAIL();
    }

    @Override
    @PostMapping("add")
    public Result add(@RequestBody TypeTemplate typeTemplate) {
        templateService.add(typeTemplate);
        return Result.SUCCESS();
    }

    @Override
    @PostMapping("update")
    public Result update(@RequestBody TypeTemplate typeTemplate) {
        templateService.update(typeTemplate);
        return Result.SUCCESS();
    }

    @Override
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Long id) {
        templateService.delete(id);
        return Result.SUCCESS();
    }

    @Override
    @DeleteMapping("deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable Long[] ids) {
        templateService.deleteByIds(ids);
        return Result.SUCCESS();
    }
}
